package org.gbif.metrics.cube.bucketer;

import java.util.List;
import java.util.UUID;

import com.google.common.collect.ImmutableList;
import com.urbanairship.datacube.BucketType;
import com.urbanairship.datacube.Bucketer;
import com.urbanairship.datacube.CSerializable;
import com.urbanairship.datacube.serializables.BytesSerializable;

/**
 * Bucketer that serializes UUIDs as 36 bytes of text.
 */
public class UUIDBucketer implements Bucketer<UUID> {
  // UUID are 36 long
  public static final int BYTES=36;
  
  @Override
  public List<BucketType> getBucketTypes() {
    return ImmutableList.of(BucketType.IDENTITY);
  }
  
  @Override
  public CSerializable bucketForWrite(UUID coordinate, BucketType bucketType) {
      return bucketInternal(coordinate, bucketType);
  }

  @Override
  public CSerializable bucketForRead(Object coordinate, BucketType bucketType) {
      return bucketInternal((UUID)coordinate, bucketType);
  }  

  public CSerializable bucketInternal(UUID c, BucketType bucketType) {
    if (bucketType != BucketType.IDENTITY) {
      throw new IllegalArgumentException("You can only use " + UUIDBucketer.class.getSimpleName()
        + " with the default identity bucketer");
    }
    return new BytesSerializable(c.toString().getBytes());
  }
}
